﻿@{
    ViewBag.Title = "Chuyển kho";
}
<fieldset>
    <legend>Tìm kiếm</legend>

    <table>
        <tr>
            <td style="width: 15%">
                <span>Mã sản phẩm :</span>
            </td>
            <td>
                @Html.TextBox("SearchMaSP")
                <input id="popupSanPham" type="button" value="+" style="width: 30px" onclick="showattach(this.id)" />

                <span>Tên sản phẩm :</span>
                @Html.TextBox("SearchTenSP", null, new { @disabled = "disabled" })
            </td>
        </tr>
        <tr>
            <td>Ngày chuyển kho:
            </td>
            <td>
                @Html.TextBox("dtFrom", ViewBag.crFr as string, new { @style = "width:100px", @placeholder = "Từ ngày" })
                @Html.TextBox("dtTo", ViewBag.crTo as string, new { @style = "width:100px", @placeholder = "Đến ngày" })
            </td>

        </tr>
        <tr>
            <td colspan="2" style="text-align: center">
                <input id="btnSearch" value="Tìm kiếm" type="button" style="padding: 10px 25px; font-size: 14pt" name="btnCTT" />
            </td>
        </tr>
    </table>
</fieldset>

<p style="padding: 0 0 20px 0">
    @Html.ActionLink("Thêm mới hóa đơn chuyển kho", "Create", null, new { @class = "linkCreate" })
</p>
<table id="jqgData"></table>
<div id="jqgpData"></div>
<div id="pageDetails" style="display: none" title="Chi tiết hóa đơn"></div>
<div id="dialog-SanPham" style="display: none" title="Sản phẩm">
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#dtFrom").datepicker();
        $("#dtTo").datepicker();
        loadGrid();
        $("#gs_Action").css("display", "none");
        $("#gs_CodeChuyen").css("display", "none");
        $("#gs_Time").css("display", "none");
    });

    //show dialog san pham
    function showattach() {
        $("#dialog-SanPham").dialog({
            width: 1000,
            height: 600,
            modal: true,
            open: function () {
                $(this).load("/SanPhamInKho/IndexSPCK");
            },
        });
    }

    function SelectSP(sp) {
        var selectedValue = sp;
        $.ajax({
            url: '@Url.Action("GetProductXKById", "SanPham")',
            type: 'POST',
            data: { "selectedValue": selectedValue },
            dataType: 'json',
            success: function (response) {
                var productDetails = response.ProductDetail;
                var productdetail = productDetails.split(';#');
                $("#SearchMaSP").val(productdetail[0]);
                $("#SearchTenSP").val(productdetail[3]);

            }
        });
        $("#dialog-SanPham").dialog("close");
    }

    $('#btnSearch').click(function (event) {
        $("#jqgData").jqGrid('setGridParam', { postData: { from: $("#dtFrom").val(), to: $("#dtTo").val(), masp: $("#SearchMaSP").val(), btn: 'Search' } });
        $('#jqgData').setGridParam({ datatype: "json" }).trigger('reloadGrid');
    });

    function loadGrid() {
        $('#jqgData').setGridParam({ datatype: "json" }).trigger('reloadGrid');
        $("#jqgData").jqGrid({
            url: '@Url.Action("IndexGetGrid", "ChuyenKho")',

            datatype: 'json',
            mtype: 'GET',
            colNames: ['ID', 'Mã hóa đơn', 'Ngày chuyển kho', 'Action'],
            colModel: [
                { name: 'ID', index: 'ID', align: 'left', hidden: true },
                { name: 'CodeChuyen', index: 'CodeChuyen', align: 'left', width: '200px', search: false },
                {
                    name: 'Time', index: 'Time', width: '110px', align: 'left', editable: false, formatter: 'date', search: false,
                    formatoptions: {
                        srcformat: 'd/m/Y H:i:s',
                        newformat: 'd/m/Y'
                    },
                    align: 'right',
                    sorttype: "date",
                    searchoptions: {
                        sopt: ['eq', 'ne'],
                        dataInit: function (elem) {
                            $(elem).datepicker({
                                dateFormat: 'dd/mm/yy',
                                onSelect: function (dateText, inst) {
                                    setTimeout(function () {
                                        $('#jqgData')[0].triggerToolbar();
                                    }, 100);
                                }
                            });
                        }
                    }
                },
                { name: 'Action', index: 'ID', align: 'center', width: '45px', formatter: ActionFormat },
            ],
            pager: '#jqgpData',
            jsonReader: { repeatitems: false },
            sortname: 'CodeChuyen',
            viewrecords: true,
            sortorder: 'desc',
            height: 'auto',
            autoencode: true,
            rowNum: 20,
            rowList: [5, 10, 20],

        });
        $("#jqgData").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn", enableClear: false, ignoreCase: true });
    }
    function ActionFormat(cellvalue, options, rowObject) {
        var rowid = rowObject["ID"];
        return "<input style='height:22px;' type='button' value='Sửa' onclick=\"window.location.href='/ChuyenKho/Edit/" + rowid + "'\"  />";

    }
</script>
